#!/usr/bin/env python
#_*_ coding:utf-8_*_

from django.shortcuts import render
from app01 import models
from django.db.models import Q
from django.core.urlresolvers import resolve

def perm_svnproject_check(request,**kwargs):
    url_obj = resolve(request.path_info)
    print("urlobj",url_obj)
    print"webusername",request.user.username
    url_name = url_obj.url_name
    print "---->urlname:",url_name
    kw = url_obj.kwargs
    print("kwargs",kw)
    print(type(kw))
    svn_perm_names = models.svn_permission.objects.all()
    for svn_perm_name in svn_perm_names:
        querpername = svn_perm_name.web_users.all()
        querperssions = svn_perm_name.svn_projects.all()
    listn = []
    listpp = []
    for n in querpername:
        strn = str(n)
        listn.append(strn)

    for pp in querperssions:
        strpp = str(pp)
        listpp.append(strpp)

    for k,v in kw.items():
        if k=='svn_id':
            print'v',v

            adf = str(models.svns.objects.get(id=v))
            print("web submit host",adf)

    print("namelist----",listn)
    print("name--pppp--",listpp)

    if request.user.username in listn and adf in listpp:

        print('-success--')
        return True
    else:
        print("----fail----")
        return False


def check_svnproject_permission(fun):
    def wapper(request, *args, **kwargs):
        if perm_svnproject_check(request, *args, **kwargs):
            return fun(request, *args, **kwargs)
        return render(request, 'forbiden.html', locals())
    return wapper
